const dateUtils = require('../../utils/defautils')
const menuData=require('../../utils/menu.js')
const {
SendverificationCode,
option,
follow,wxbind,wxunbind
} = require('../../api/login.js')
const {
levelOption,
sheng,
update,
session
} = require('../../api/index.js')
const utils = require("../../utils/index.js")
Page({
data: {
code:'',
openid:'',
tableName:"",
userInfo:'',
ro:{
},
username:'',
password:'',
role:'',
xuehao:'',
mima:'',
xueshengxingming:'',
xingbieList:"男,女".split(','),
xingbieIndex:0,

xueyuan:'',
zhuanye:'',
nianji:'',
xueliList:"小学,初中,高中,中专,大专,本科,研究生,博士".split(','),
xueliIndex:0,

shifoubiyeList:"是,否".split(','),
shifoubiyeIndex:0,

biyenianfen:'',
xiangmufuzerenList:"是,否".split(','),
xiangmufuzerenIndex:0,

suozaixuexiao:'',
youxiang:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
chuangyelingyu:'',
canjiahuodong:'',
canyuxiangmu:'',
leixingList:"在校生,毕业生".split(','),
leixingIndex:0,

chengshi:'',
yuanqu:'',
jiedao:'',
email:'',
daoshigonghao:'',
mima:'',
daoshixingming:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
lianxidianhua:'',
xingbieList:"男,女".split(','),
xingbieIndex:0,

zhicheng:'',
xiangmufenlei:'',
leixingList:"校内,校外".split(','),
leixingIndex:0,

zhuanyejingyan:'',
zhidaojingyan:'',
xueshubeijing:'',
suozaidanwei:'',
youxiang:'',
suozaiyuanxi:'',
gerenjianjie:'',
lingyuzhuanzhang:'',
xiangmuleixing:'',
zhidaoguoxiangmu:'',
gongzuojingli:'',
fuzerenzhanghao:'',
mima:'',
fuzerenxingming:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
xingbieList:"男,女".split(','),
xingbieIndex:0,

shoujihaoma:'',
shijizhanghao:'',
mima:'',
xingming:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
xingbieList:"男,女".split(','),
xingbieIndex:0,

shoujihaoma:'',
youxiang:'',
zhiwei:'',
chengshi:'',
qujizhanghao:'',
mima:'',
xingming:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
xingbieList:"男,女".split(','),
xingbieIndex:0,

shoujihaoma:'',
youxiang:'',
zhiwei:'',
chengshi:'',
yuanqu:'',
jiedaozhanghao:'',
mima:'',
xingming:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
xingbieList:"男,女".split(','),
xingbieIndex:0,

shoujihaoma:'',
youxiang:'',
zhiwei:'',
chengshi:'',
yuanqu:'',
jiedao:'',
xuexiaozhanghao:'',
mima:'',
xuexiaomingcheng:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
xingbieList:"男,女".split(','),
xingbieIndex:0,

shoujihaoma:'',
youxiang:'',
zhiwei:'',
chengshi:'',
xueyuanzhanghao:'',
mima:'',
xuexiaomingcheng:'',
xueyuanmingcheng:'',
touxiang:'../../static/upload.png',
tempPathtouxiang:'../../static/upload.png',
xingbieList:"男,女".split(','),
xingbieIndex:0,

shoujihaoma:'',
youxiang:'',
zhiwei:'',
chengshi:'',

registerContainerClass: "",
role:wx.getStorageSync("nowTable"),
showForceModal: false, // 强制补全信息弹窗

},

async onLoad() {

wx.login({
provider: 'weixin',
success: function (res) {
let code = res.code;
if (code) {
this.setData({
code
});
}
}.bind(this)
});
let tableName = wx.getStorageSync("nowTable");
const  userInfo  = getApp().globalData.userInfo
    this.setData({
        tableName,
        userInfo
    })
const   baseURL= wx.getStorageSync('baseURL')+"/"
    let userInfoObj={}
    
// 获取城市数据
const chengshi = await option('chengshi/chengshi')
let chengshiIndex
chengshi.data?.map((item, index) => {
if (item == userInfo.chengshi) {
chengshiIndex = index
}
})
this.setData({
chengshiIndex,
chengshiList: chengshi.data
})

// 获取园区数据
const yuanqu = await option('yuanqu/yuanqu')
let yuanquIndex
yuanqu.data?.map((item, index) => {
if (item == userInfo.yuanqu) {
yuanquIndex = index
}
})
this.setData({
yuanquIndex,
yuanquList: yuanqu.data
})

// 获取街道数据
const jiedao = await option('jiedao/jiedao')
let jiedaoIndex
jiedao.data?.map((item, index) => {
if (item == userInfo.jiedao) {
jiedaoIndex = index
}
})
this.setData({
jiedaoIndex,
jiedaoList: jiedao.data
})

// 设置项目分类数据
const xiangmufenlei = await option('xiangmufenlei/xiangmufenlei')
let xiangmufenleiIndex
xiangmufenlei.data?.map((item, index) => {
if (item == userInfo.xiangmufenlei) {
xiangmufenleiIndex = index
}
})
this.setData({
xiangmufenleiIndex,
xiangmufenleiList: xiangmufenlei.data
})

// 设置其他下拉列表索引值
if (userInfo.xingbie) {
    let xingbieIndex = this.data.xingbieList.findIndex(item => item === userInfo.xingbie)
    this.setData({ xingbieIndex: xingbieIndex !== -1 ? xingbieIndex : 0 })
}
if (userInfo.xueli) {
    let xueliIndex = this.data.xueliList.findIndex(item => item === userInfo.xueli)
    this.setData({ xueliIndex: xueliIndex !== -1 ? xueliIndex : 0 })
}
if (userInfo.xiangmufuzeren) {
    let xiangmufuzerenIndex = this.data.xiangmufuzerenList.findIndex(item => item === userInfo.xiangmufuzeren)
    this.setData({ xiangmufuzerenIndex: xiangmufuzerenIndex !== -1 ? xiangmufuzerenIndex : 0 })
}
if (userInfo.shifoubiye) {
    let shifoubiyeIndex = this.data.shifoubiyeList.findIndex(item => item === userInfo.shifoubiye)
    this.setData({ shifoubiyeIndex: shifoubiyeIndex !== -1 ? shifoubiyeIndex : 0 })
}
if (userInfo.leixing) {
    let leixingIndex = this.data.leixingList.findIndex(item => item === userInfo.leixing)
    this.setData({ leixingIndex: leixingIndex !== -1 ? leixingIndex : 0 })
}

    //普
                                
    userInfoObj["username"]= userInfo.username==null?"":userInfo.username
    //普
                                
    userInfoObj["password"]= userInfo.password==null?"":userInfo.password
    //普
                                
    userInfoObj["role"]= userInfo.role==null?"":userInfo.role
    //普
                                
    userInfoObj["xuehao"]= userInfo.xuehao==null?"":userInfo.xuehao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["xueshengxingming"]= userInfo.xueshengxingming==null?"":userInfo.xueshengxingming
    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                
    userInfoObj["xueyuan"]= userInfo.xueyuan==null?"":userInfo.xueyuan
    //普
                                
    userInfoObj["zhuanye"]= userInfo.zhuanye==null?"":userInfo.zhuanye
    //普
                                
    userInfoObj["nianji"]= userInfo.nianji==null?"":userInfo.nianji
    //下
                                
    userInfoObj["xueli"]= userInfo.xueli==null?"":userInfo.xueli
    //下
                                
    userInfoObj["shifoubiye"]= userInfo.shifoubiye==null?"":userInfo.shifoubiye
    //普
                                
    userInfoObj["biyenianfen"]= userInfo.biyenianfen==null?"":userInfo.biyenianfen
    //下
                                
    userInfoObj["xiangmufuzeren"]= userInfo.xiangmufuzeren==null?"":userInfo.xiangmufuzeren
    //普
                                
    userInfoObj["suozaixuexiao"]= userInfo.suozaixuexiao==null?"":userInfo.suozaixuexiao
    //普
                                this.setData({
youxiang: userInfo.youxiang,
})
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //普
                                
    userInfoObj["chuangyelingyu"]= userInfo.chuangyelingyu==null?"":userInfo.chuangyelingyu
    //普
                                
    userInfoObj["canjiahuodong"]= userInfo.canjiahuodong==null?"":userInfo.canjiahuodong
    //普
                                
    userInfoObj["canyuxiangmu"]= userInfo.canyuxiangmu==null?"":userInfo.canyuxiangmu
    //下
                                
    userInfoObj["leixing"]= userInfo.leixing==null?"":userInfo.leixing
    //下
                                
    userInfoObj["chengshi"]= userInfo.chengshi==null?"":userInfo.chengshi
    //下
                                
    userInfoObj["yuanqu"]= userInfo.yuanqu==null?"":userInfo.yuanqu
    //下
                                
    userInfoObj["jiedao"]= userInfo.jiedao==null?"":userInfo.jiedao
    //普
                                this.setData({
email: userInfo.email,
})
    //普
                                
    userInfoObj["daoshigonghao"]= userInfo.daoshigonghao==null?"":userInfo.daoshigonghao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["daoshixingming"]= userInfo.daoshixingming==null?"":userInfo.daoshixingming
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //普
                                this.setData({
lianxidianhua: userInfo.lianxidianhua,
})
    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                
    userInfoObj["zhicheng"]= userInfo.zhicheng==null?"":userInfo.zhicheng
    //下
                                
    userInfoObj["xiangmufenlei"]= userInfo.xiangmufenlei==null?"":userInfo.xiangmufenlei
    //下
                                
    userInfoObj["leixing"]= userInfo.leixing==null?"":userInfo.leixing
    //普
                                
    userInfoObj["zhuanyejingyan"]= userInfo.zhuanyejingyan==null?"":userInfo.zhuanyejingyan
    //普
                                
    userInfoObj["zhidaojingyan"]= userInfo.zhidaojingyan==null?"":userInfo.zhidaojingyan
    //普
                                
    userInfoObj["xueshubeijing"]= userInfo.xueshubeijing==null?"":userInfo.xueshubeijing
    //普
                                
    userInfoObj["suozaidanwei"]= userInfo.suozaidanwei==null?"":userInfo.suozaidanwei
    //普
                                this.setData({
youxiang: userInfo.youxiang,
})
    //普
                                
    userInfoObj["suozaiyuanxi"]= userInfo.suozaiyuanxi==null?"":userInfo.suozaiyuanxi
    //普
                                
    userInfoObj["gerenjianjie"]= userInfo.gerenjianjie==null?"":userInfo.gerenjianjie
    //普
                                
    userInfoObj["lingyuzhuanzhang"]= userInfo.lingyuzhuanzhang==null?"":userInfo.lingyuzhuanzhang
    //普
                                
    userInfoObj["xiangmuleixing"]= userInfo.xiangmuleixing==null?"":userInfo.xiangmuleixing
    //普
                                
    userInfoObj["zhidaoguoxiangmu"]= userInfo.zhidaoguoxiangmu==null?"":userInfo.zhidaoguoxiangmu
    //普
                                
    userInfoObj["gongzuojingli"]= userInfo.gongzuojingli==null?"":userInfo.gongzuojingli
    //普
                                
    userInfoObj["fuzerenzhanghao"]= userInfo.fuzerenzhanghao==null?"":userInfo.fuzerenzhanghao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["fuzerenxingming"]= userInfo.fuzerenxingming==null?"":userInfo.fuzerenxingming
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                this.setData({
shoujihaoma: userInfo.shoujihaoma,
})
    //普
                                
    userInfoObj["shijizhanghao"]= userInfo.shijizhanghao==null?"":userInfo.shijizhanghao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["xingming"]= userInfo.xingming==null?"":userInfo.xingming
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                this.setData({
shoujihaoma: userInfo.shoujihaoma,
})
    //普
                                this.setData({
youxiang: userInfo.youxiang,
})
    //普
                                
    userInfoObj["zhiwei"]= userInfo.zhiwei==null?"":userInfo.zhiwei
    //下
                                
    userInfoObj["chengshi"]= userInfo.chengshi==null?"":userInfo.chengshi
    //普
                                
    userInfoObj["qujizhanghao"]= userInfo.qujizhanghao==null?"":userInfo.qujizhanghao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["xingming"]= userInfo.xingming==null?"":userInfo.xingming
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                this.setData({
shoujihaoma: userInfo.shoujihaoma,
})
    //普
                                this.setData({
youxiang: userInfo.youxiang,
})
    //普
                                
    userInfoObj["zhiwei"]= userInfo.zhiwei==null?"":userInfo.zhiwei
    //下
                                
    userInfoObj["chengshi"]= userInfo.chengshi==null?"":userInfo.chengshi
    //下
                                
    userInfoObj["yuanqu"]= userInfo.yuanqu==null?"":userInfo.yuanqu
    //普
                                
    userInfoObj["jiedaozhanghao"]= userInfo.jiedaozhanghao==null?"":userInfo.jiedaozhanghao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["xingming"]= userInfo.xingming==null?"":userInfo.xingming
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                this.setData({
shoujihaoma: userInfo.shoujihaoma,
})
    //普
                                this.setData({
youxiang: userInfo.youxiang,
})
    //普
                                
    userInfoObj["zhiwei"]= userInfo.zhiwei==null?"":userInfo.zhiwei
    //下
                                
    userInfoObj["chengshi"]= userInfo.chengshi==null?"":userInfo.chengshi
    //下
                                
    userInfoObj["yuanqu"]= userInfo.yuanqu==null?"":userInfo.yuanqu
    //普
                                
    userInfoObj["jiedao"]= userInfo.jiedao==null?"":userInfo.jiedao
    //普
                                
    userInfoObj["xuexiaozhanghao"]= userInfo.xuexiaozhanghao==null?"":userInfo.xuexiaozhanghao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["xuexiaomingcheng"]= userInfo.xuexiaomingcheng==null?"":userInfo.xuexiaomingcheng
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                this.setData({
shoujihaoma: userInfo.shoujihaoma,
})
    //普
                                this.setData({
youxiang: userInfo.youxiang,
})
    //普
                                
    userInfoObj["zhiwei"]= userInfo.zhiwei==null?"":userInfo.zhiwei
    //下
                                
    userInfoObj["chengshi"]= userInfo.chengshi==null?"":userInfo.chengshi
    //普
                                
    userInfoObj["xueyuanzhanghao"]= userInfo.xueyuanzhanghao==null?"":userInfo.xueyuanzhanghao
    //普
                                
    userInfoObj["mima"]= userInfo.mima==null?"":userInfo.mima
    //普
                                
    userInfoObj["xuexiaomingcheng"]= userInfo.xuexiaomingcheng==null?"":userInfo.xuexiaomingcheng
    //普
                                
    userInfoObj["xueyuanmingcheng"]= userInfo.xueyuanmingcheng==null?"":userInfo.xueyuanmingcheng
    //图
                                userInfo['touxiang']=userInfo?.touxiang?.replace('upload','file')
this.setData({
touxiang:baseURL+userInfo.touxiang,
tempPathtouxiang:baseURL+userInfo.touxiang,
})

    //下
                                
    userInfoObj["xingbie"]= userInfo.xingbie==null?"":userInfo.xingbie
    //普
                                this.setData({
shoujihaoma: userInfo.shoujihaoma,
})
    //普
                                this.setData({
youxiang: userInfo.youxiang,
})
    //普
                                
    userInfoObj["zhiwei"]= userInfo.zhiwei==null?"":userInfo.zhiwei
    //下
                                
    userInfoObj["chengshi"]= userInfo.chengshi==null?"":userInfo.chengshi
    userInfoObj['role']=wx.getStorageSync("nowTable");
    this.setData(
        userInfoObj
    )
//ss读取

},
checkUserInfoComplete() {
    // 你可以根据实际必填项调整
    const requiredFields = [
        'xueshengxingming', 'xingbie', 'chengshi', 'zhuanye', 'xueli', 'shifoubiye',
        'youxiang', 'shoujihaoma'
    ];
    let infoComplete = true;
    for (const field of requiredFields) {
        if (!this.data[field] || this.data[field] === '' || this.data[field] === undefined) {
            infoComplete = false;
            break;
        }
    }
    if (!infoComplete) {
        if (!this.data.showForceModal) {
            this.setData({ showForceModal: true });
            if (this.forceModalTimer) clearTimeout(this.forceModalTimer);
            this.forceModalTimer = setTimeout(() => {
                this.setData({ showForceModal: false });
                // 1秒后再次检测，如果信息还不全，弹窗会再次出现
                setTimeout(() => { this.checkUserInfoComplete(); }, 1000);
            }, 60000); // 1分钟
        }
        return false;
    } else {
        if (this.data.showForceModal) {
            this.setData({ showForceModal: false });
            if (this.forceModalTimer) clearTimeout(this.forceModalTimer);
        }
        return true;
    }
},
async onShow() {
    this.getSession();
    // 每次进入页面都强制弹窗
    if (this.forceModalTimer) clearTimeout(this.forceModalTimer);
    this.setData({ showForceModal: true });
    this.forceModalTimer = setTimeout(() => {
        this.setData({ showForceModal: false });
        // 1秒后检测信息完整性，不全则再次弹窗
        setTimeout(() => { this.checkUserInfoComplete(); }, 1000);
    }, 60000);
},
onInputChange(e) {
  const field = e.currentTarget.dataset.field;
  this.setData({
    [field]: e.detail.value
  }, () => {
    this.checkUserInfoComplete();
  });
},
xingbieChange(e) {
const selectedIndex = e.detail.value;
this.setData({
xingbie:this.data.xingbieList[selectedIndex],
xingbieIndex: selectedIndex,
});
},
xueliChange(e) {
const selectedIndex = e.detail.value;
this.setData({
xueli:this.data.xueliList[selectedIndex],
xueliIndex: selectedIndex,
});
},
shifoubiyeChange(e) {
const selectedIndex = e.detail.value;
this.setData({
shifoubiye:this.data.shifoubiyeList[selectedIndex],
shifoubiyeIndex: selectedIndex,
});
},
xiangmufuzerenChange(e) {
const selectedIndex = e.detail.value;
this.setData({
xiangmufuzeren:this.data.xiangmufuzerenList[selectedIndex],
xiangmufuzerenIndex: selectedIndex,
});
},
async sendCodeHandler() {
if (!this.data.canSendCode) {
return;
}
if (this.data.youxiang == "") {
wx.showToast({
title: '请输入邮箱',
icon: 'none'
})
return;
} else {
if (validatePhoneNumber(this.data.youxiang) == false) {
wx.showToast({
title: '请输入有效邮箱',
icon: 'none'
})
return;
} else {
const res = await SendverificationCode("${tableName}", 'sendsms','youxiang',  this.data.youxiang)
this.setData({
smscode: res.data
})
}
}
this.setData({
canSendCode: false,
});
let time = this.data.countdown;
let timer = setInterval(() => {
time--;
this.setData({
countdown: time, // 更新倒计时的时间
});
if (time <= 0) {
clearInterval(timer); // 倒计时结束，清除定时器
this.setData({
buttonText: '发送验证码',
canSendCode: true,
countdown: 60
});
}
}, 1000);

},
touxiangTap() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
tempPathtouxiang: tempFilePaths[0]
})
// 上传网络图片
const  baseURL=  wx.getStorageSync("baseURL")
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
if (result.code == 0) {
this.setData({
        touxiang: 'file/' + result.file
})
} else {
wx.showToast({
    title: result.msg,
    icon: 'none',
    duration: 2000
});
}
}
})



}
})
},
leixingChange(e) {
const selectedIndex = e.detail.value;
this.setData({
leixing:this.data.leixingList[selectedIndex],
leixingIndex: selectedIndex,
});
},
chengshiChange(e) {
const selectedIndex = e.detail.value;
this.setData({
chengshi:this.data.chengshiList[selectedIndex],
chengshiIndex: selectedIndex,
});
},
yuanquChange(e) {
const selectedIndex = e.detail.value;
this.setData({
yuanqu:this.data.yuanquList[selectedIndex],
yuanquIndex: selectedIndex,
});
},
jiedaoChange(e) {
const selectedIndex = e.detail.value;
this.setData({
jiedao:this.data.jiedaoList[selectedIndex],
jiedaoIndex: selectedIndex,
});
},
async sendCodeHandler() {
if (!this.data.canSendCode) {
return;
}
if (this.data.email == "") {
wx.showToast({
title: '请输入邮箱',
icon: 'none'
})
return;
} else {
if (validatePhoneNumber(this.data.email) == false) {
wx.showToast({
title: '请输入有效邮箱',
icon: 'none'
})
return;
} else {
const res = await SendverificationCode("${tableName}", 'sendemail','email',  this.data.email)

this.setData({
smscode: res.data
})
}
}
this.setData({
canSendCode: false,
});
let time = this.data.countdown;
let timer = setInterval(() => {
time--;
this.setData({
countdown: time, // 更新倒计时的时间
});
if (time <= 0) {
clearInterval(timer); // 倒计时结束，清除定时器
this.setData({
buttonText: '发送验证码',
canSendCode: true,
countdown: 60
});
}
}, 1000);

},
touxiangTap() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
tempPathtouxiang: tempFilePaths[0]
})
// 上传网络图片
const  baseURL=  wx.getStorageSync("baseURL")
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
if (result.code == 0) {
this.setData({
        touxiang: 'file/' + result.file
})
} else {
wx.showToast({
    title: result.msg,
    icon: 'none',
    duration: 2000
});
}
}
})



}
})
},
async sendCodeHandler() {
if (!this.data.canSendCode) {
return;
}
if (this.data.lianxidianhua == "") {
wx.showToast({
title: '请输入联系电话',
icon: 'none'
})
return;
} else {
if (validatePhoneNumber(this.data.lianxidianhua) == false) {
wx.showToast({
title: '请输入有效联系电话',
icon: 'none'
})
return;
} else {
const res = await SendverificationCode("${tableName}", 'sendsms','lianxidianhua',  this.data.lianxidianhua)
this.setData({
smscode: res.data
})
}
}
this.setData({
canSendCode: false,
});
let time = this.data.countdown;
let timer = setInterval(() => {
time--;
this.setData({
countdown: time, // 更新倒计时的时间
});
if (time <= 0) {
clearInterval(timer); // 倒计时结束，清除定时器
this.setData({
buttonText: '发送验证码',
canSendCode: true,
countdown: 60
});
}
}, 1000);

},
xingbieChange(e) {
const selectedIndex = e.detail.value;
this.setData({
xingbie:this.data.xingbieList[selectedIndex],
xingbieIndex: selectedIndex,
});
},
xiangmufenleiChange(e) {
const selectedIndex = e.detail.value;
this.setData({
xiangmufenlei:this.data.xiangmufenleiList[selectedIndex],
xiangmufenleiIndex: selectedIndex,
});
},
leixingChange(e) {
const selectedIndex = e.detail.value;
this.setData({
leixing:this.data.leixingList[selectedIndex],
leixingIndex: selectedIndex,
});
},
async sendCodeHandler() {
if (!this.data.canSendCode) {
return;
}
if (this.data.youxiang == "") {
wx.showToast({
title: '请输入邮箱',
icon: 'none'
})
return;
} else {
if (validatePhoneNumber(this.data.youxiang) == false) {
wx.showToast({
title: '请输入有效邮箱',
icon: 'none'
})
return;
} else {
const res = await SendverificationCode("${tableName}", 'sendsms','youxiang',  this.data.youxiang)
this.setData({
smscode: res.data
})
}
}
this.setData({
canSendCode: false,
});
let time = this.data.countdown;
let timer = setInterval(() => {
time--;
this.setData({
countdown: time, // 更新倒计时的时间
});
if (time <= 0) {
clearInterval(timer); // 倒计时结束，清除定时器
this.setData({
buttonText: '发送验证码',
canSendCode: true,
countdown: 60
});
}
}, 1000);

},
touxiangTap() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
tempPathtouxiang: tempFilePaths[0]
})
// 上传网络图片
const  baseURL=  wx.getStorageSync("baseURL")
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
if (result.code == 0) {
this.setData({
        touxiang: 'file/' + result.file
})
} else {
wx.showToast({
    title: result.msg,
    icon: 'none',
    duration: 2000
});
}
}
})



}
})
},
xingbieChange(e) {
const selectedIndex = e.detail.value;
this.setData({
xingbie:this.data.xingbieList[selectedIndex],
xingbieIndex: selectedIndex,
});
},
async sendCodeHandler() {
if (!this.data.canSendCode) {
return;
}
if (this.data.shoujihaoma == "") {
wx.showToast({
title: '请输入手机号码',
icon: 'none'
})
return;
} else {
if (validatePhoneNumber(this.data.shoujihaoma) == false) {
wx.showToast({
title: '请输入有效手机号码',
icon: 'none'
})
return;
} else {
const res = await SendverificationCode("${tableName}", 'sendsms','shoujihaoma',  this.data.shoujihaoma)
this.setData({
smscode: res.data
})
}
}
this.setData({
canSendCode: false,
});
let time = this.data.countdown;
let timer = setInterval(() => {
time--;
this.setData({
countdown: time, // 更新倒计时的时间
});
if (time <= 0) {
clearInterval(timer); // 倒计时结束，清除定时器
this.setData({
buttonText: '发送验证码',
canSendCode: true,
countdown: 60
});
}
}, 1000);

},
touxiangTap() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
tempPathtouxiang: tempFilePaths[0]
})
// 上传网络图片
const  baseURL=  wx.getStorageSync("baseURL")
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
if (result.code == 0) {
this.setData({
        touxiang: 'file/' + result.file
})
} else {
wx.showToast({
    title: result.msg,
    icon: 'none',
    duration: 2000
});
}
}
})



}
})
},
xingbieChange(e) {
const selectedIndex = e.detail.value;
this.setData({
xingbie:this.data.xingbieList[selectedIndex],
xingbieIndex: selectedIndex,
});
},
async sendCodeHandler() {
if (!this.data.canSendCode) {
return;
}
if (this.data.shoujihaoma == "") {
wx.showToast({
title: '请输入手机号码',
icon: 'none'
})
return;
} else {
if (validatePhoneNumber(this.data.shoujihaoma) == false) {
wx.showToast({
title: '请输入有效手机号码',
icon: 'none'
})
return;
} else {
const res = await SendverificationCode("${tableName}", 'sendsms','shoujihaoma',  this.data.shoujihaoma)
this.setData({
smscode: res.data
})
}
}
this.setData({
canSendCode: false,
});
let time = this.data.countdown;
let timer = setInterval(() => {
time--;
this.setData({
countdown: time, // 更新倒计时的时间
});
if (time <= 0) {
clearInterval(timer); // 倒计时结束，清除定时器
this.setData({
buttonText: '发送验证码',
canSendCode: true,
countdown: 60
});
}
}, 1000);

},
async sendCodeHandler() {
if (!this.data.canSendCode) {
return;
}
if (this.data.youxiang == "") {
wx.showToast({
title: '请输入邮箱',
icon: 'none'
})
return;
} else {
if (validatePhoneNumber(this.data.youxiang) == false) {
wx.showToast({
title: '请输入有效邮箱',
icon: 'none'
})
return;
} else {
const res = await SendverificationCode("${tableName}", 'sendsms','youxiang',  this.data.youxiang)
this.setData({
smscode: res.data
})
}
}
this.setData({
canSendCode: false,
});
let time = this.data.countdown;
let timer = setInterval(() => {
time--;
this.setData({
countdown: time, // 更新倒计时的时间
});
if (time <= 0) {
clearInterval(timer); // 倒计时结束，清除定时器
this.setData({
buttonText: '发送验证码',
canSendCode: true,
countdown: 60
});
}
}, 1000);

},
chengshiChange(e) {
const selectedIndex = e.detail.value;
this.setData({
chengshi:this.data.chengshiList[selectedIndex],
chengshiIndex: selectedIndex,
});
},
quitTap(){
let saveBaseURL = wx.getStorageSync('baseURL')

wx.clearStorageSync();
wx.setStorageSync('baseURL', saveBaseURL)
wx.reLaunch({
url: "/pages/login/login"
});
},
async  saveTap(){
  const baseURL = wx.getStorageSync('baseURL') + "/"
  const regex = new RegExp(baseURL, "g");
  // 只保留必要字段，去除重复和无用字段
  const resultObj = {
    id: getApp().globalData.userInfo?.id,
    username: this.data.username,
    password: this.data.password,
    role: this.data.role,
    xuehao: this.data.xuehao,
    xueshengxingming: this.data.xueshengxingming,
    xingbie: this.data.xingbie ? this.data.xingbie : this.data.xingbieList[this.data.xingbieIndex],
    xueyuan: this.data.xueyuan,
    zhuanye: this.data.zhuanye,
    nianji: this.data.nianji,
    xueli: this.data.xueli ? this.data.xueli : this.data.xueliList[this.data.xueliIndex],
    shifoubiye: this.data.shifoubiye ? this.data.shifoubiye : this.data.shifoubiyeList[this.data.shifoubiyeIndex],
    biyenianfen: this.data.biyenianfen,
    xiangmufuzeren: this.data.xiangmufuzeren ? this.data.xiangmufuzeren : this.data.xiangmufuzerenList[this.data.xiangmufuzerenIndex],
    suozaixuexiao: this.data.suozaixuexiao,
    youxiang: this.data.youxiang,
    touxiang: this.data.touxiang.replace(regex, ""),
    chuangyelingyu: this.data.chuangyelingyu,
    canjiahuodong: this.data.canjiahuodong,
    canyuxiangmu: this.data.canyuxiangmu,
    leixing: this.data.leixing ? this.data.leixing : this.data.leixingList[this.data.leixingIndex],
    chengshi: this.data.chengshi ? this.data.chengshi : this.data.chengshiList[this.data.chengshiIndex],
    yuanqu: this.data.yuanqu ? this.data.yuanqu : this.data.yuanquList?.[this.data.yuanquIndex],
    jiedao: this.data.jiedao ? this.data.jiedao : this.data.jiedaoList?.[this.data.jiedaoIndex],
    email: this.data.email,
    daoshigonghao: this.data.daoshigonghao,
    daoshixingming: this.data.daoshixingming,
    lianxidianhua: this.data.lianxidianhua,
    zhicheng: this.data.zhicheng,
    xiangmufenlei: this.data.xiangmufenlei ? this.data.xiangmufenlei : this.data.xiangmufenleiList[this.data.xiangmufenleiIndex],
    zhuanyejingyan: this.data.zhuanyejingyan,
    zhidaojingyan: this.data.zhidaojingyan,
    xueshubeijing: this.data.xueshubeijing,
    suozaidanwei: this.data.suozaidanwei,
    suozaiyuanxi: this.data.suozaiyuanxi,
    gerenjianjie: this.data.gerenjianjie,
    lingyuzhuanzhang: this.data.lingyuzhuanzhang,
    xiangmuleixing: this.data.xiangmuleixing,
    zhidaoguoxiangmu: this.data.zhidaoguoxiangmu,
    gongzuojingli: this.data.gongzuojingli,
    fuzerenzhanghao: this.data.fuzerenzhanghao,
    fuzerenxingming: this.data.fuzerenxingming,
    shoujihaoma: this.data.shoujihaoma,
    shijizhanghao: this.data.shijizhanghao,
    xingming: this.data.xingming,
    zhiwei: this.data.zhiwei,
    qujizhanghao: this.data.qujizhanghao,
    jiedaozhanghao: this.data.jiedaozhanghao,
    xuexiaozhanghao: this.data.xuexiaozhanghao,
    xuexiaomingcheng: this.data.xuexiaomingcheng,
    xueyuanzhanghao: this.data.xueyuanzhanghao,
    xueyuanmingcheng: this.data.xueyuanmingcheng
  };
  // 前端必填校验
  // 判断页面是否有用户名输入框（users表才有）
  const needUsername = this.data.tableName === 'users';
  const hasXingming = !!(resultObj.xingming || resultObj.xueshengxingming);
  if ((needUsername && (!resultObj.username || !hasXingming)) || (!needUsername && !hasXingming)) {
    wx.showToast({ title: needUsername ? '用户名和姓名不能为空' : '姓名不能为空', icon: 'none' });
    return;
  }
  // 过滤掉 undefined 和空字符串字段
  Object.keys(resultObj).forEach(key => {
    if (resultObj[key] === undefined || resultObj[key] === "") delete resultObj[key]
  })
  console.log('提交数据', resultObj)
  const role = wx.getStorageSync('nowTable')
  const res = await update(role, resultObj)
  console.log('update返回', res)
  if (res.code == 0) {
    const userInfoRes = await session(role)
    if (userInfoRes.code == 0) {
      getApp().globalData.userInfo = userInfoRes.data
      wx.reLaunch({
        url: '/pages/center/center',
      })
    }
  } else {
    wx.showToast({
      title: res.msg,
      icon: "none"
    })
  }
},
async   getSession(){
const table= wx.getStorageSync('nowTable')
let res = await session(table);
getApp().globalData.userInfo=res.data;
this.setData({
openid:res?.data?.openid,
})
}
});